package com.ymatou.c2cliveindexer.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.c2cliveindexer.parameter.KeywordSearchBean;
import com.ymatou.c2cliveindexer.parameter.LiveSearchReturnBean;
import com.ymatou.c2cliveindexer.service.KeywordSearchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtC2CLiveActivity;
import com.ymttest.database.sqlwapper.YmtC2CLiveActivityWapper;

/**
 * c2clive关键字搜索接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_KeywordSearch {
	/*static LiveindexerBean liveindexerBean;
	static LiveindexerCall liveindexerCall;*/
	private static KeywordSearchCall keywordSearchCall;
	private static KeywordSearchBean keywordSearchBean;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("c2clive关键字和首页列表搜索接口");
		try {
			
			//Thread.sleep(5000);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		keywordSearchBean = new KeywordSearchBean();
		keywordSearchCall = new KeywordSearchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(keywordSearchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_KeywordSearch_001() {
		Logger.start(true,"关键字搜索数据验证");
		try {
			String keyword = "美国";
			
			keywordSearchBean.setFl("id,isReplay,keyword");
			keywordSearchBean.setLimit("0,1000");
			keywordSearchBean.setQuery("keyword(keyword,"+ keyword + ")");
			keywordSearchCall.setData(keywordSearchBean);
			
			keywordSearchCall.callService();
		
			List<HashMap<String, String>> hists = new ArrayList<HashMap<String, String>>();
			hists = keywordSearchCall.getHitsList();
			Logger.verifyEquals("OK", keywordSearchCall.getStatus(), "验证接口返回状态");
			Logger.verifyEquals(keywordSearchCall.getTotal(), hists.size(), "验证接口返回数量 和 数据库 中查出的数据数量");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			// 将数据库的id加到list里
			ArrayList<Integer> id = new ArrayList<Integer>();
			for (YmtC2CLiveActivity a : list) {
				id.add(a.getIactivityid());
			}
			
			if (hists.size() > 0) {
				for (HashMap h : hists) {
					Logger.verifyEquals(true, h.get("keyword").toString().contains(keyword), "验证返回字符串是否包含关键字");
				}
			}
			// 验证根据keywork 查询出来的结果集在数据库有效数据里
			if (hists.size() > 0) {
				for (HashMap h : hists) {
					// Logger.debug("id:"+h.get("id"));
					Logger.verifyEquals(true, id.contains(Integer.valueOf((String) (h.get("id")))),"验证id:" + h.get("id") + "跟数据库一致");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_KeywordSearch_002() {
		Logger.start(true,"关键字搜索成功");
		try {
			String keyword = "日本";
			keywordSearchCall.setData("query=keyword(keyword," + keyword
					+ ")&fl=id,isReplay,keyword&limit=0,500&sort=desc(yscore)");
			keywordSearchCall.callService();
			Logger.comment("发送报文:" + keywordSearchCall.getEntityString());
			Logger.comment("返回报文:" + keywordSearchCall.getReturnData());
			List<HashMap<String, String>> hists = new ArrayList<HashMap<String, String>>();
			hists = keywordSearchCall.getHitsList();
			Logger.verifyEquals("OK", keywordSearchCall.getStatus(), "验证返回状态");
			Logger.verifyEquals(keywordSearchCall.getTotal(), hists.size(), "验证接口返回数量 和 数据库 中查出的数据数量");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			// 将数据库的id加到list里
			ArrayList<Integer> id = new ArrayList<Integer>();
			for (YmtC2CLiveActivity a : list) {
				id.add(a.getIactivityid());
			}
			// Logger.debug(hists);
			if (hists.size() > 0) {
				for (HashMap h : hists) {
					Logger.verifyEquals(true, h.get("keyword").toString().contains(keyword), "验证返回字符串包含关键字");
				}
			}
			// 验证根据keywork 查询出来的结果集在数据库有效数据里
			if (hists.size() > 0) {
				for (HashMap h : hists) {
					// Logger.debug("id:"+h.get("id"));
					Logger.verifyEquals(true, id.contains(Integer.valueOf((String) (h.get("id")))),
							"验证id:" + h.get("id") + "跟数据库一致");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_KeywordSearch_003(){
		Logger.start(true,"搜索首页列表返回结果及数据验证");
		try {
			//Thread.sleep(3000);
			keywordSearchCall
					.setData("query=*&fl=id,isReplay,activityName,content,location,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,100");
			// liveSearchCall.setData("");

			keywordSearchCall.callService();
			// liveSearchCall.callService();
			Thread.sleep(2000);
			Logger.debug("发送报文:" + keywordSearchCall.getEntityString());
			Logger.debug("返回报文:" + keywordSearchCall.getReturnData());
			Logger.verifyEquals("OK", keywordSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			List<LiveSearchReturnBean> returnList = keywordSearchCall.getHitsList2();
	
			// 升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(keywordSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).getIactivityid(), returnList.get(i).getId(), "验证数据库与返回的ctivityid");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_KeywordSearch_004() {
		Logger.start(true,"搜索首页列表成功");
		try {
			keywordSearchCall
					.setData("fl=id,isReplay,activityName,content,location,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,500&sort=desc(yscore)");
			keywordSearchCall.callService();
			// liveSearchCall.callService();
			Thread.sleep(2000);
			Logger.debug("发送报文:" + keywordSearchCall.getEntityString());
			Logger.debug("返回报文:" + keywordSearchCall.getReturnData());
			Logger.verifyEquals("OK", keywordSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			ArrayList<LiveSearchReturnBean> returnList = keywordSearchCall.getHitsList2();
			// 升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(keywordSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).getIactivityid(), returnList.get(i).getId(), "验证数据库与返回的ctivityid");
				}

			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}